package com.pug.zixun.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

/**
 * @Description: redis 配置
 * @Author: WengYu
 * @CreateTime: 2022/05/28 01:16
 */

@Configuration
public class RedisConfiguration {


    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        // 1、创建一个 redisTemplate
        RedisTemplate<String , Object> redisTemplate = new RedisTemplate<>();
        // 2、设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 3、创建一个json序列化方式
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置key和value序列化
        redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        // 设置hash key和value序列化
        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        // 设置默认调用
        redisTemplate.afterPropertiesSet();


        return redisTemplate;
    }
}
